package com.phantasy.scene.sceneData
{
	import flash.utils.ByteArray;
	import flash.utils.Dictionary;
	import flash.utils.getTimer;

	public class MapEncode
	{
		public function MapEncode()
		{
		}
		
		public static function encode(_arg1:MapDataVo):ByteArray
		{
			var _local7:Array;
			var _local10:int;
			var _local11:MapElementVo;
			var _local12:ByteArray;
			var _local13:MapTransferVo;
			var _local14:ByteArray;
			var _local2:int = (_arg1.offsetX + MapDataVo.CORRECT_VALUE);
			var _local3:int = (_arg1.offsetY + MapDataVo.CORRECT_VALUE);
			var _local4:int = _arg1.width;
			var _local5:int = _arg1.height;
			var _local6:ByteArray = new ByteArray();
			_local6.writeInt(_arg1.map_id);
			_local6.writeInt(_arg1.isSub);
			_local6.writeMultiByte(_arg1.name, "cn-gb");
			_local6.position = 40;
			_local6.writeMultiByte(_arg1.imageLink, "cn-gb");
			_local6.position = 72;
			_local6.writeInt(_arg1.tileRow);
			_local6.writeInt(_arg1.tileCol);
			_local6.writeInt(_arg1.elements.length);
			_local6.writeInt(_arg1.transfers.length);
			_local6.writeInt(_local2);
			_local6.writeInt(_local3);
			_local6.writeInt(_local4);
			_local6.writeInt(_local5);
			var _local8:int;
			while (_local8 < _arg1.tiles.length)
			{
				_local7 = _arg1.tiles[_local8];
				_local10 = 0;
				while (_local10 < _local7.length)
				{
					_local6.writeByte(_local7[_local10]);
					_local10++;
				};
				_local8++;
			};
			var _local9:int;
			while (_local9 < _arg1.elements.length)
			{
				_local11 = _arg1.elements[_local9];
				_local6.writeInt(_local11.id);
				_local6.writeInt(_local11.tx);
				_local6.writeInt(_local11.ty);
				_local6.writeInt(_local11.itemType);
				_local12 = new ByteArray();
				_local12.writeMultiByte(_local11.avatarId, "cn-gb");
				_local6.writeInt(_local12.length);
				_local6.writeMultiByte(_local11.avatarId, "cn-gb");
				_local9++;
			};
			_local9 = 0;
			while (_local9 < _arg1.transfers.length)
			{
				_local13 = _arg1.transfers[_local9];
				_local6.writeInt(_local13.id);
				_local6.writeInt(_local13.tx);
				_local6.writeInt(_local13.ty);
				_local6.writeInt(_local13.tar_Map);
				_local6.writeInt(_local13.tar_tx);
				_local6.writeInt(_local13.tar_ty);
				_local6.writeInt(_local13.hw);
				_local6.writeInt(_local13.yl);
				_local6.writeInt(_local13.wl);
				_local6.writeInt(_local13.minLevel);
				_local6.writeInt(_local13.maxLevel);
				_local14 = new ByteArray();
				_local14.writeMultiByte(_local13.avatarId, "cn-gb");
				_local6.writeInt(_local14.length);
				_local6.writeMultiByte(_local13.avatarId, "cn-gb");
				_local9++;
			};
			_local6.compress();
			return (_local6);
		}
		
		public static function encodeByteArray(bytes:ByteArray):MapDataVo
		{
			var arr:Array;
			var z:int;
			var ele:MapElementVo;
			var l:int;
			var tran:MapTransferVo;
			var len:int;
			var t:int = getTimer();
			var hash:Dictionary = new Dictionary();
			bytes.position = 0;
			try
			{
				bytes.uncompress();
			}
			catch(e:Error)
			{
			};
			var vo:MapDataVo = new MapDataVo();
			vo.map_id = bytes.readInt();
			vo.isSub = bytes.readInt();
			vo.name = bytes.readMultiByte(32, "cn-gb");
			vo.imageLink = bytes.readMultiByte(32, "cn-gb");
			vo.tileRow = bytes.readInt();
			vo.tileCol = bytes.readInt();
			var ele_length:int = bytes.readInt();
			var tran_length:int = bytes.readInt();
			vo.offsetX = (bytes.readInt() - MapDataVo.CORRECT_VALUE);
			vo.offsetY = (bytes.readInt() - MapDataVo.CORRECT_VALUE);
			vo.width = bytes.readInt();
			vo.height = bytes.readInt();
			var tiles:Array = new Array(vo.tileRow);
			var x:int;
			while (x < tiles.length)
			{
				arr = new Array(vo.tileCol);
				z = 0;
				while (z < arr.length)
				{
					arr[z] = bytes.readUnsignedByte();
					z = (z + 1);
				};
				tiles[x] = arr;
				x = (x + 1);
			};
			vo.tiles = tiles;
			var elements:Vector.<MapElementVo> = new Vector.<MapElementVo>();
			var j:int;
			while (j < ele_length)
			{
				ele = new MapElementVo();
				ele.id = bytes.readInt();
				ele.tx = bytes.readInt();
				ele.ty = bytes.readInt();
				ele.itemType = bytes.readInt();
				l = bytes.readInt();
				ele.avatarId = bytes.readMultiByte(l, "cn-gb");
				elements.push(ele);
				j = (j + 1);
			};
			vo.elements = elements;
			var transfers:Vector.<MapTransferVo> = new Vector.<MapTransferVo>();
			var k:int;
			while (k < tran_length)
			{
				tran = new MapTransferVo();
				tran.id = bytes.readInt();
				tran.tx = bytes.readInt();
				tran.ty = bytes.readInt();
				tran.tar_Map = bytes.readInt();
				tran.tar_tx = bytes.readInt();
				tran.tar_ty = bytes.readInt();
				tran.hw = bytes.readInt();
				tran.yl = bytes.readInt();
				tran.wl = bytes.readInt();
				tran.minLevel = bytes.readInt();
				tran.maxLevel = bytes.readInt();
				len = bytes.readInt();
				tran.avatarId = bytes.readMultiByte(len, "cn-gb");
				transfers.push(tran);
				k = (k + 1);
			};
			vo.transfers = transfers;
			return (vo);
		}
	}
}